
package com.zhou.core.scheduler;

import com.zhou.core.scheduler.factory.ThreadNameFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 负责管理线程
 * 自带ScheduledThreadPoolExecutor处理
 *  Created by zhouyongjun on 2020/8/26
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class GameThreadPoolExecutor {
    private final static Logger log = LoggerFactory.getLogger(GameJobScheduler.class);
    private ScheduledThreadPoolExecutor executor;
    private final static GameThreadPoolExecutor instance = new GameThreadPoolExecutor();


//    //世界主线程
//    private WorldThread mainThread = null;
//    //定时保存玩家线程
//    private SaveThread saveThread = null;
    //	private DailyZeroable dailyZeroAble = null;//每日0点线程
//	private DailyThreePointable dailyFivePointAble = null;//每日3点线程
//    private IntegralPointRunnable integralPointRunnable = null;//整点执行线程

    public GameThreadPoolExecutor() {

    }
    public static GameThreadPoolExecutor getInstance() {return instance;}

    public ScheduledFuture<?> schedule(Runnable command) {

        return schedule(command,0,TimeUnit.MILLISECONDS);
    }
    public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
        try {
            if (executor.isShutdown()) return null;
            return executor.schedule(command, delay, unit);
        } catch (Exception e) {
            log.error("",e);
        }
        return null;
    }

    /**
     * 创建一个给定初始延迟的间隔性的任务，之后的每次任务执行时间为 初始延迟 + N * delay(间隔)  。
     * @param command
     * @param initialDelay
     * @param period
     * @param unit
     * @return
     */
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                    long initialDelay,
                                                    long period,
                                                    TimeUnit unit) {
        if (executor.isShutdown()) return null;
        return executor.scheduleAtFixedRate(command, initialDelay, period, unit);
    }

    /**
     * 创建一个给定初始延迟的间隔性的任务，之后的下次执行时间是上一次任务从执行到结束所需要的时间+给定的间隔时间
     * 下次执行包含本次执行时间
     * @param command
     * @param initialDelay
     * @param period
     * @param unit
     * @return
     */
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit) {
        if (executor.isShutdown()) return null;
        return executor.scheduleWithFixedDelay(command, initialDelay, period, unit);
    }

    /**
     * 删除
     * @param runnable
     */
    public void remove(Runnable runnable) {
        try {
            executor.remove(runnable);
        } catch (Exception e) {
            log.error("",e);
        }
    }


    public void init() throws Exception {
        executor = new ScheduledThreadPoolExecutor(30,new ThreadNameFactory("gameThreadPool-"));
        log.debug(" init...");
    }


    public void startup() throws Exception {
        log.debug(" startup...");
    }

    public void shutdown() throws Exception {
        executor.shutdown();
        log.debug(" shutdown...");
    }
}
